package tree.tree2.symmetry.maxAndMinTree;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * @ClassName MaxDepth
 * @Description TODO
 * @Author lenovo
 * @Date 2023-04-27 10:58
 * @Version 1.0
 * @Comment Magic. Do not touch.
 * If this comment is removed. the program will blow up
 */
public class NTreeMaxDepth {

    /**
     * 给定一个 N 叉树，找到其最大深度。
     * <p>
     * 最大深度是指从根节点到最远叶子节点的最长路径上的节点总数。
     * <p>
     * N 叉树输入按层序遍历序列化表示，每组子节点由空值分隔（请参见示例）。
     *
     * @param root 1
     *             2   2
     * @return
     */
    public int maxDepth(Node root) {
        if (root == null) return 0;
        List<Node> children = root.children;
        int maxDepth = 0;
        for (Node child : children) {
            int childDepth = maxDepth(child);
            maxDepth = Math.max(maxDepth, childDepth);
        }
        return maxDepth + 1;
    }

    public int maxDepth1(Node root) {
        int res = 0;
        if (root == null) {
            return res;
        }
        Queue<Node> queue = new LinkedList<>();
        queue.add(root);
        while (!queue.isEmpty()) {
            int size = queue.size();
            while (size > 0) {
                Node poll = queue.poll();
                List<Node> children = poll.children;
                for (Node child : children) {
                    queue.add(child);
                }
                size--;
            }
            res++;
        }
        return res;
    }


    public static void main(String[] args) {
        Node root = new Node(1);

        Node root1 = new Node(2);
        Node root2 = new Node(2);
        List<Node> child = new ArrayList<>();
        child.add(root1);
        child.add(root2);
        root.children = child;
        NTreeMaxDepth nTreeMaxDepth = new NTreeMaxDepth();
        int i = nTreeMaxDepth.maxDepth1(root);
    }
}